package com.wude.guild.imageutil.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.liji.imagezoom.util.ImageZoom;
import com.wude.guild.imageutil.R;
import com.wude.guild.imageutil.face.ImageDelFace;
import com.wude.guild.imageutil.face.PhotoFace;
import com.wude.guild.imageutil.face.ShowImagesFace;
import com.wude.guild.imageutil.util.PhotoUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 唐小妹 on 2017/12/6 0006.
 */

public class PhotoViewGroup extends LinearLayout implements ImageDelFace, PhotoFace, ShowImagesFace {
    private Context context;
    private LinearLayout ll_content;
    private GroupLayout groupLayout;
    private MyImageView imageView;
    private int imageCount;
    private static final int TAKEPHOTO = 11;
    private static final int CALLPHOTO = 22;
    private PhotoUtil photoUtil;

    private MyImageView currImageView;
    private List<Bitmap> bitmaps;
    private int max;//最大张数
    private List<String> imgUrls;

    public PhotoViewGroup(Context context, PhotoUtil photoUtil, int max) {
        super(context);
        this.context = context;
        this.photoUtil = photoUtil;
        this.max = max;

        initView();
    }

    public PhotoViewGroup(Context context, List<String> imgUrls) {
        super(context);
        this.context = context;
        this.imgUrls = imgUrls;

        initView();
    }

    private void initView() {
        bitmaps = new ArrayList<>();

        LayoutInflater.from(context).inflate(R.layout.tc_photo, this);

        ll_content = findViewById(R.id.ll_content);
        groupLayout = new GroupLayout(context);
        ll_content.addView(groupLayout);

        if (imgUrls == null)
            addView();
        else
            addView(imgUrls);
    }

    private void addView() {
        imageView = new MyImageView(context, this, this, this);
        imageView.setTag(System.currentTimeMillis());
        groupLayout.addView(imageView);
        currImageView = imageView;
        imageCount++;
    }

    private void addView(List<String> imgUrls) {
        for (int i = 0; i < imgUrls.size(); i++) {
            imageView = new MyImageView(context, this, this, this);
            imageView.setTag(i);
            imageView.loadImg(imgUrls.get(i));
            groupLayout.addView(imageView);
            currImageView = imageView;
            imageCount++;
        }
    }

    public List<Bitmap> getBitmaps() {
        return bitmaps;
    }

    @Override
    public void delImg(long tag) {
        for (int i = 0; i < groupLayout.getChildCount(); i++) {
            MyImageView imageView = (MyImageView) groupLayout.getChildAt(i);
            long ctag = (long) imageView.getTag();
            if (ctag == tag) {
                groupLayout.removeView(imageView);
                imageCount--;
                bitmaps.remove(i);
                if (bitmaps.size() == groupLayout.getChildCount())
                    addView();
            }
        }
    }

    @Override
    public void takePhoto() {
        photoUtil.takePhoto(TAKEPHOTO);
    }

    @Override
    public void cellPhoto() {
        photoUtil.callPhoto(CALLPHOTO);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Bitmap bitmap = null;
        if (requestCode == TAKEPHOTO) {
            bitmap = photoUtil.getTakePhoto();
        } else if (requestCode == CALLPHOTO) {
            bitmap = photoUtil.getCallPhoto(data);
        }
        if (bitmap == null)
            return;
        currImageView.setImageBitmap(bitmap);
        bitmaps.add(bitmap);
        if (max == 0 || (max != 0 && imageCount < max))
            addView();
    }

    @Override
    public void showImages(int position) {
        ImageZoom.show(context, position, imgUrls);
    }
}
